package com.garen.app.biz;

import com.garen.app.jframe.TomcatFrame;
import com.garen.utils.CommonUtils;
import com.garen.utils.FileUtils;
import com.garen.utils.LangUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.Socket;

/*
 * Tomcat软件管理
 * 1 端口查询
 * 2 设置端口
 * 3 启动
 * 4 停止
 * 5 升级...保留
 * 
 * 剩余问题:
 * 端口检测
 * 打开默认网页
 * 端口自动计算
 * 启动监控状态
 * */
public class TomcatBiz {

    private static Log log = LogFactory.getLog(TomcatBiz.class);

    private TomcatFrame jframe;

    private String status;//服务状态

    private String basePath;
    private String jreName;
    private String tomcatName;
    private boolean flag;//启动标志

    public TomcatBiz(String path, String jreName, String tomcatName, TomcatFrame jframe) {
        this.basePath = path;
        this.jreName = jreName;
        this.tomcatName = tomcatName;
        this.jframe = jframe;
    }

    //启动
    public void start() {
        status = "start";
        Runnable run = new Runnable() {
            @Override
            public void run() {
                Runtime rt = Runtime.getRuntime();
                String cmd = basePath + "/" + tomcatName + "/bin/catalina.bat run";
                String[] envp = {"JRE_HOME=" + basePath + "/" + jreName};
                File baseDir = new File(basePath + "/" + tomcatName);
                try {
                    Process process = rt.exec(cmd, envp, baseDir);
                    byte[] b = FileUtils.readbodydata(process.getErrorStream(), 0);
                    if (flag) {
                        flag = false;
                        status = "stop";
                        return;//手工停止服务
                    }
                    String msg = new String(b, "gbk");
                    String msgTxt = "";
                    if (msg.indexOf("JVM_Bind") > 0) {
                        msgTxt = "端口冲突";
                    }
                    JOptionPane.showMessageDialog(jframe, "服务启动失败 !" + msgTxt,
                            "提示信息", JOptionPane.WARNING_MESSAGE);
                    jframe.updateStatus(1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(run).start();
    }

    //停止
    public void stop() {
        flag = true;
        Runtime rt = Runtime.getRuntime();
        String cmd = basePath + "/" + tomcatName + "/bin/catalina.bat stop";
        String[] envp = {"JRE_HOME=" + basePath + "/" + jreName};
        File baseDir = new File(basePath + "/" + tomcatName);
        try {
            Process process = rt.exec(cmd, envp, baseDir);
            byte[] b = FileUtils.readbodydata(process.getInputStream(), 0);
            log.debug(new String(b, "gbk"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询端口:1 http 2 管理
    public int queryPort(int type) {
        String path = basePath + "/" + tomcatName + "/conf/server.xml";
        String rule = type == 1 ? "Service.Connector[@port]" : "[@port]";
        return LangUtils.s2int(CommonUtils.readXMLValueEx(path, rule));
    }

    //查询服务状态
    public boolean checkStatus() {
        return checkPort(queryPort(1));
    }

    //检测端口是否占用
    public static boolean checkPort(int port) {
        boolean ret = false;
        if (port <= 0) return false;
        try {
            Socket clientSocket = new Socket("127.0.0.1", port);
            ret = true;
            clientSocket.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return ret;
    }
}
